package com.mckillion.icare;
//The purpose of this class is to send a text message if the perimeter is breached.

//Import the required java packages.
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.preference.PreferenceManager;
import android.telephony.SmsManager;

//This class extends BroadcastReceiver and to monitor and receive context.
public class ProximityIntentReciever extends BroadcastReceiver {
	
	//Declare the variables that will be used within the class.
	SharedPreferences prefs;
	String key, contactNumber;
	boolean atHome;
	
	@Override
	public void onReceive(Context context, Intent intent){
		
		//Get preference values from sharedPreferences so that we can send SMS to the contact.
		prefs = PreferenceManager.getDefaultSharedPreferences(context);
		contactNumber = prefs.getString("primary_number","Nothing has been entered");
		
		//Set the key string to match the KEY_PROXIMITY_ENTERING value.
		key = LocationManager.KEY_PROXIMITY_ENTERING;
		
		//Create boolean which determines whether the device is within the proximity.
		//this uses an intent and the key string from above.
		atHome = intent.getBooleanExtra(key, false);
		
		if(atHome){
			//If the device is within the defined area nothing need to happen.
			System.out.println("Device is within home perimeter"); //used for testing purposes.
		}	
		
		else
			{   if(HomeScreen.messageSent2 == false)
				{	//If the message has not been sent and the device is not at home then send the text.
					SmsManager smsManager = SmsManager.getDefault();
				    String smsText = "ALERT! The User of iCare has left the locational boundary.";
				    smsManager.sendTextMessage(contactNumber, null, smsText, null, null);
				    
				    //Change the boolean to stop multiple texts.
				    HomeScreen.messageSent2 = true;
				}
			}
	}
}
